/**
 * The MIT License (MIT)
 * 时钟特效
 * @author 透笔度
 * @开源中国 https://my.oschina.net/tbd/blog
 * @码云 https://gitee.com/dgx
 */

const towxImg = require('../../../utils/towxImg');
// pages/clockeffect2/clockeffect2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //基本换算数据
    cdeg: -90,
    b1: 30,           //h
    b2: 6,             //m
    cb2: 1 / 2,
    cb22: 1 / 120,
    b3: 6,              //s
    cb3: 1 / 10,
    //字符换时分秒
    hh: null,
    mm: null,
    ss: null,
    //图形时分秒
    deghh: null,
    degmm: null,
    degss: null,
    article: '',
    myInterval1: null,
    myInterval2: null,
  },
  //初始化时间
  initc: function () {
    //获取当前时间
    var nowdate = new Date();
    var hh = (nowdate.getHours() > 12) ? nowdate.getHours() - 12 : nowdate.getHours();
    var mm = nowdate.getMinutes();
    var ss = nowdate.getSeconds();
    //设置
    this.setData({ hh: hh, mm: mm, ss: ss });
    //换算出对应角度
    var deghh = this.data.b1 * hh + mm * this.data.cb2 + ss * this.data.cb22 + this.data.cdeg;
    var degmm = this.data.b2 * mm + ss * this.data.cb3 + this.data.cdeg;
    var degss = this.data.b3 * ss + this.data.cdeg;
    //设置
    this.setData({ deghh: deghh, degmm: degmm, degss: degss });
  },
  //定时器时间
  interc: function () {
    var that = this;
    //时分动效
    let myInterval1 = setInterval(function () {
      that.hmanimate();
    }, 1000);
    //秒动效
    let myInterval2 = setInterval(function () {
      that.sanimate();
    }, 1000 / 12);

    that.setData({
      myInterval1,
      myInterval2
    })
  },
  //时分动效
  hmanimate: function () {
    //获取当前时间
    var nowdate = new Date();
    var hh = (nowdate.getHours() > 12) ? nowdate.getHours() - 12 : nowdate.getHours();
    var mm = nowdate.getMinutes();
    var ss = nowdate.getSeconds();
    //设置
    this.setData({ hh: hh, mm: mm, ss: ss });
    //换算出对应角度
    var deghh = this.data.b1 * hh + mm * this.data.cb2 + ss * this.data.cb22 + this.data.cdeg;
    var degmm = this.data.b2 * mm + ss * this.data.cb3 + this.data.cdeg;
    //设置
    this.setData({ deghh: deghh, degmm: degmm });
  },
  //秒动效
  sanimate: function () {
    this.setData({ degss: this.data.degss + 0.5 });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //初始化时间
    this.initc();
    //定时器时间
    this.interc();

    let code = '```\n<view class="container my-page">\n  <view>时钟特效（指针平滑）</view>\n  <view class="center">\n    <view id="wordClock">{{hh}}:{{mm}}:{{ss}}</view>\n    <view id="chartClock">\n      <image class="chartClock-back" mode="scaleToFill" src="../../../images/clockeffect/0cback1.png"></image>\n      <view id="hh" style="transform:rotate({{deghh}}deg);">\n        <image class="chartClock-back" mode="scaleToFill" src="../../../images/clockeffect/0ch1.png"></image>\n      </view>\n      <view id="mm" style="transform:rotate({{degmm}}deg);">\n        <image class="chartClock-back" mode="scaleToFill" src="../../../images/clockeffect/0cm1.png"></image>\n      </view>\n      <view id="ss" style="transform:rotate({{degss}}deg);">\n        <image class="chartClock-back" mode="scaleToFill" src="../../../images/clockeffect/0cs1.png"></image>\n      </view>\n    </view>\n  </view>\n</view>\n```\n\n```\nPage({\n  /**\n   * 页面的初始数据\n   */\n  data: {\n    //基本换算数据\n    cdeg: -90,\n    b1: 30,           //h\n    b2: 6,             //m\n    cb2: 1 / 2,\n    cb22: 1 / 120,\n    b3: 6,              //s\n    cb3: 1 / 10,\n    //字符换时分秒\n    hh: null,\n    mm: null,\n    ss: null,\n    //图形时分秒\n    deghh: null,\n    degmm: null,\n    degss: null\n  },\n  //初始化时间\n  initc: function () {\n    //获取当前时间\n    var nowdate = new Date();\n    var hh = (nowdate.getHours() > 12) ? nowdate.getHours() - 12 : nowdate.getHours();\n    var mm = nowdate.getMinutes();\n    var ss = nowdate.getSeconds();\n    //设置\n    this.setData({ hh: hh, mm: mm, ss: ss });\n    //换算出对应角度\n    var deghh = this.data.b1 * hh + mm * this.data.cb2 + ss * this.data.cb22 + this.data.cdeg;\n    var degmm = this.data.b2 * mm + ss * this.data.cb3 + this.data.cdeg;\n    var degss = this.data.b3 * ss + this.data.cdeg;\n    //设置\n    this.setData({ deghh: deghh, degmm: degmm, degss: degss });\n  },\n  //定时器时间\n  interc: function () {\n    var that = this;\n    //时分动效\n    setInterval(function () {\n      that.hmanimate();\n    }, 1000);\n    //秒动效\n    setInterval(function () {\n      that.sanimate();\n    }, 1000 / 12);\n  },\n  //时分动效\n  hmanimate:function(){\n    //获取当前时间\n    var nowdate = new Date();\n    var hh = (nowdate.getHours() > 12) ? nowdate.getHours() - 12 : nowdate.getHours();\n    var mm = nowdate.getMinutes();\n    var ss = nowdate.getSeconds();\n    //设置\n    this.setData({ hh: hh, mm: mm, ss: ss });\n    //换算出对应角度\n    var deghh = this.data.b1 * hh + mm * this.data.cb2 + ss * this.data.cb22 + this.data.cdeg;\n    var degmm = this.data.b2 * mm + ss * this.data.cb3 + this.data.cdeg;\n    //设置\n    this.setData({ deghh: deghh, degmm: degmm });\n  },\n  //秒动效\n  sanimate: function () {\n    this.setData({ degss: this.data.degss + 0.5 });\n  },\n  /**\n   * 生命周期函数--监听页面加载\n   */\n  onLoad: function (options) {\n    //初始化时间\n    this.initc();\n    //定时器时间\n    this.interc();\n  },\n})\n```\n';

    let article = towxImg.dealWithRichText(code);
    this.setData({ article });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let { myInterval1, myInterval2 } = this.data;
    clearInterval(myInterval1);
    clearInterval(myInterval2);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})